<style scoped>
.button-padding{padding: 20px 10px;}
</style>
<template>
  <HeaderSlot>
    <van-nav-bar :title="$t(this_action.label)" left-arrow @click-left="href_handle(-1)" />
  </HeaderSlot>
  <form_param ref="form_param" :config="form_config" :submit="submit">
    <template #submit v-if="this_action.type !== 'info'">
      <div class="button-padding">
        <van-button block round type="primary" native-type="submit">{{ this_action.label }}</van-button>
      </div>
    </template>
  </form_param>
</template>

<script>
import form_param from "@/components/form_param.vue"
import {ajax, pay_handle} from "@/js/api_tool.js";
import {_get, href_handle} from "@/vue_plugin/router_auto.js";
import HeaderSlot from "@/components/slot/HeaderSlot.vue";

export default {
  components: {HeaderSlot, form_param},
  props: {
    model: Object,
    this_action: Object,
    is_admin: Boolean,
  },
  data(){
    return {
      id: _get("id",0),
      form_config: [],
    }
  },
  methods:{
    href_handle,
    submit(form_data){
      return new Promise((resolve, reject)=>{
        let url = `/${this.is_admin?'admin':'home'}/${this.model.name}/${this.this_action.action}?id=${this.id}`;
        if (this.this_action.type === "pay"){
          pay_handle(url, form_data, ()=>{
            href_handle(-1, 1000);
          },(res)=>{
            if (res?.errors) reject(res.errors);
          });
        }else {
          ajax({url:url, data:form_data, loading:1, method:'POST'}, (res)=>{
            setTimeout(()=>{
              console.log(this.this_action.type);
              if (this.this_action.type === "add") {
                return this.$refs.form_param.init();
              }
              href_handle(-1);
            }, 1000);
          },(res)=>{
            reject(res.errors)
          })
        }
      })
    },
  },
  computed:{},
  mounted(){
    let url = `/home/${this.model.name}/${this.this_action.action}?id=${this.id}&_is_init_=1`
    ajax(url, (res)=>{
      let extend_config = {};
      if (this.this_action.type === 'info') extend_config.is_info = true;
      this.form_config = Object.values(res.data).map(row=>{
        Object.assign(row, extend_config);
        return row;
      });

      // 记录表单初始值
      setTimeout(()=>{
        this.$refs.form_param.set_init_value();
      },200)
    })
  }
}
</script>